module executor.group.server.GroupExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import hunt.util.Serialize;
import client.MsgServerClient;
import client.ImClient;
import client.LogicServerClient;
import std.array;
import std.conv : to;
import core.stdc.time;

@TransportServer(gateServerName)
class GroupExecutor : AbstractExecutor!(GroupExecutor){

  @MessageId(MESSAGE.CMD_GREATE_GROUP_REQ)
  void createGroup(TransportContext ctx, MessageBuffer buffer) {
    auto req = new CreateGroupREQ();
    buffer.data.fromProtobuf!CreateGroupREQ(req);
    req.createTime  = core.stdc.time.time(null);
    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_GREATE_GROUP_REQ,req.toProtobuf.array));
    logInfo("createGroup");
  }

  @MessageId(MESSAGE.CMD_DEAL_GROUP_REQ)
  void dealGroupApply(TransportContext ctx, MessageBuffer buffer) {
    auto req = new DealApplyGroup();
    buffer.data.fromProtobuf!DealApplyGroup(req);
    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_DEAL_GROUP_REQ,req.toProtobuf.array));
    logInfo("dealGroupApply");
  }


  @MessageId(MESSAGE.CMD_GROUP_INVITATION)
  void groupInvitation(TransportContext ctx, MessageBuffer buffer) {
    auto req = new InviteGroup();
    buffer.data.fromProtobuf!InviteGroup(req);
    //MessageTransportClient client = MsgServerClient.getInstance.getForwardClient(req.destination);
    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_INVITE_GROUP,req.toProtobuf.array));
    logInfo("groupInvitation");
  }

  @MessageId(MESSAGE.CMD_LEAVE_GROUP)
  void leaveGroup(TransportContext ctx, MessageBuffer buffer) {
    auto req = new LeaveGroup();
    buffer.data.fromProtobuf!LeaveGroup(req);
    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_LEAVE_GROUP,req.toProtobuf.array));
    logInfof("leaveGroup  %s ---- %s",req.fromId, req.groupId);
  }

  @MessageId(MESSAGE.CMD_CANCEL_GROUP)
  void cancelGroup(TransportContext ctx, MessageBuffer buffer) {
    auto req = new CancelGroup();
    buffer.data.fromProtobuf!CancelGroup(req);
    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_CANCEL_GROUP,req.toProtobuf.array));
    logInfof("cancelGroup  %s ---- %s",req.fromId, req.groupId);
  }

  @MessageId(MESSAGE.CMD_KICK_USER)
  void kickUser(TransportContext ctx, MessageBuffer buffer) {
    auto req = new KickUser();
    buffer.data.fromProtobuf!KickUser(req);
    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_KICK_USER,req.toProtobuf.array));
    logInfof("kickUser  %s ---- %s",req.fromId, req.groupId);
  }

  //@MessageId(MESSAGE.CMD_APPLY_GROUP)
  //void applyGroup(TransportContext ctx, MessageBuffer buffer) {
  //  auto req = new ApplyGroup();
  //  buffer.data.fromProtobuf!ApplyGroup(req);
  //  LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_APPLY_GROUP,req.toProtobuf.array));
  //  logInfof("applyGroup  %s ---- %s",req.fromId, req.groupId);
  //}

  @MessageId(MESSAGE.CMD_UPDATE_GROUP_INFO)
  void updateGroupInfo(TransportContext ctx, MessageBuffer buffer) {
    auto req = new UpdateGroupInfo();
    buffer.data.fromProtobuf!UpdateGroupInfo(req);
    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_UPDATE_GROUP_INFO,req.toProtobuf.array));
    logInfof("updateGroupInfo  %s ---- %s",req.fromId, req.groupId);
  }


  @MessageId(MESSAGE.CMD_SET_GROUP_VALIDATION)
  void setGroupValidation(TransportContext ctx, MessageBuffer buffer) {
    auto req = new SetGroupValidation();
    buffer.data.fromProtobuf!SetGroupValidation(req);
    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_SET_GROUP_VALIDATION,req.toProtobuf.array));
    logInfof("setGroupValidation  %s ---- %d",req.groupId, req.validation);
  }

  @MessageId(MESSAGE.CMD_SET_GROUP_NAME_REQ)
  void setGroupName(TransportContext ctx, MessageBuffer buffer) {
    auto req = new SetGroupNameREQ();
    buffer.data.fromProtobuf!SetGroupNameREQ(req);
    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_SET_GROUP_NAME_REQ,req.toProtobuf.array));
    logInfof("setGroupName  %s ---- %d",req.groupId, req.groupName);
  }


}

